package com.hx.demo.handle;

import com.alibaba.fastjson.JSONObject;
import com.hx.demo.domain.User;
import com.hx.demo.util.AjaxResult;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {


    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        User user = (User)authentication.getPrincipal();
        String currentUser = user.getUsername();
        logger.info("用户"+currentUser+"退出登录");
        response.setStatus(200);
        response.setContentType("application/json;charset=UTF-8");
        AjaxResult ajaxResult = AjaxResult.success();
        response.getWriter().append(JSONObject.toJSONString(ajaxResult));
        response.getWriter().close();;
    }
}
